home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / gp / utils.c < prev   
C/C++ Source or Header  |  1991-12-21  |  11KB  |  607 lines

  1. #include "7plus.h"
  2. #include "globals.h"
  3.  
  4. /*
  5. *** get a line from file. don't care about type of line separator.
  6. ***
  7. ***
  8. ***
  9.  */
  10.  
  11. char *my_fgets (char *string, register n, FILE *rein)
  12. {
  13.   register in, i;
  14.  
  15.   while ((in = fgetc (rein)) != EOF)
  16.   {
  17.     if (in == 0x0d || in == 0x0a)
  18.       continue;
  19.     else
  20.       break;
  21.   }
  22.   if (feof (rein))
  23.     return (NULL);
  24.  
  25.   string[0] = (char) in;
  26.   i = 1;
  27.   while ((in = fgetc (rein)) != EOF)
  28.   {
  29.     if (in == 0x0d || in == 0x0a)
  30.       in = 0x0a;
  31.     string[i++] = (char) in;
  32.     if (i == n || in == 0x0a)
  33.       break;
  34.   }
  35.   string[i] = EOS;
  36.   return (string);
  37.  
  38. /*
  39. *** Get crc and line number from code line.
  40. ***
  41. ***
  42.  */
  43.  
  44. void crc_n_lnum (uint *crc, int *linenumber, char *line)
  45. {
  46.   register ulong cs;
  47.  
  48.   cs = 0xb640L * decode[(byte)line[66]] +
  49.        0xd8L   * decode[(byte)line[65]] +
  50.                  decode[(byte)line[64]];
  51.  
  52.   *linenumber = (int) (cs >> 14);   /* upper 9 bits are the line number */
  53.   *crc = (uint) (cs & 0x3fffL);     /* lower 14 bits are the CRC */
  54. }
  55.  
  56. /*
  57. *** Get crc2 from code line.
  58. ***
  59. ***
  60.  */
  61.  
  62. void crc2 (uint *crc, char *line)
  63. {
  64.  
  65.   *crc = 0xd8 * decode[(byte)line[68]] +
  66.                 decode[(byte)line[67]];
  67. }
  68.  
  69. /*
  70. *** Whip up 2nd CRC
  71. ***
  72. ***
  73.  */
  74. void add_crc2 (char *line)
  75. {
  76.   register uint crc;
  77.   register int i;
  78.  
  79.   /* Whip up 2nd CRC */
  80.   crc = 0;
  81.   for (i=66;i>-1;i--)
  82.     crc = crctab[crc>>8] ^ (((crc&255)<<8) | (byte) line[i]);
  83.   crc &= 0x7fff;
  84.  
  85.   i = 67;
  86.   line[i++] = code[crc % 0xd8];
  87.   line[i++] = code[crc / 0xd8];
  88.   line[i] = EOS;
  89. }
  90.  
  91. /*
  92. *** mini-crc for header. safe enough...
  93. ***
  94. ***
  95.  */
  96. int mcrc (char *line, int flag)
  97. {
  98.   register int i, j;
  99.   register uint crc;
  100.   char test[3], *p;
  101.  
  102.   sprintf (test, "\xb0\xb1");
  103.  
  104.   if ((p = strstr (line, test)) == NULL)
  105.     return (0);
  106.  
  107.   j = (int) (p - line) + 4;
  108.  
  109.   for (i=crc=0; i<j; i++)
  110.     crc = crctab[crc>>8] ^ (((crc&255)<<8) | (byte)line[i]);
  111.   crc %= 216;
  112.   if (!flag)
  113.   {
  114.     if (crc == (uint) decode[(byte)line[j]])
  115.       return (1);
  116.     else
  117.       return (0);
  118.   }
  119.   else
  120.     line[j] = code[(byte)crc];
  121.  
  122.   return (crc);
  123. }
  124.  
  125. /*
  126. *** read a file, search for s1, calculate CRC until s2 is found.
  127. *** flag == 1: compare calculated an read CRC.
  128. *** flag == 0: append CRC to file.
  129.  */
  130. int crc_file (char *file, char *s1, char *s2, int flag)
  131. {
  132.   char line[81];
  133.   uint crc, cs;
  134.   int i, j, k;
  135.   FILE *in;
  136.  
  137.   crc = cs = 0;
  138.  
  139.   if ((in = fopen (file, OPEN_READ_TEXT)) == NULL)
  140.   {
  141.     printf (cant, file);
  142.     exit (2);
  143.   }
  144.  
  145.   i = (int) strlen (s1);
  146.   k = (int) strlen (s2);
  147.  
  148.   j = 1;
  149.  
  150.   do
  151.   {
  152.     if (my_fgets (line, 80, in) == NULL)
  153.       break;
  154.  
  155.     j = strncmp (line, s1, i);
  156.   }
  157.   while (j);
  158.  
  159.   if (j)
  160.   {
  161.     printf ("\nStart '%s' not found.\n", s1);
  162.     exit (0);
  163.   }
  164.  
  165.   j = 0;
  166.  
  167.   do
  168.   {
  169.     for (i=0;i!=(int)strlen(line);i++)
  170.       crc = crctab[crc>>8] ^ (((crc&255)<<8) | (byte)line[i]);
  171.  
  172.     j = strncmp (line, s2, k);
  173.  
  174.     if (!j)
  175.       continue;
  176.  
  177.     if (my_fgets (line, 80, in) == NULL)
  178.       break;
  179.   }
  180.   while (j);
  181.  
  182.  
  183.   if (j)
  184.   {
  185.     printf ("\nEnd '%s' not found.\n", s2);
  186.     exit (0);
  187.   }
  188.  
  189.   my_fgets (line, 80, in);
  190.  
  191.   fclose (in);
  192.  
  193.   /* evaluate CRC */
  194.   if (flag)
  195.   {
  196.     if (!line || strncmp ("CRC ", line, 4))
  197.     {
  198.       printf ("\n'%s': no CRC found.\n(File may be corrupted or from version \
  199. earlier than 7PLUS v1.5)\n", file);
  200.       return (2);
  201.     }
  202.     cs = get_hex (line+4);
  203.     if (cs == crc)
  204.       return (0);
  205.     else
  206.     {
  207.       printf ("\007'%s' is corrupted. Break.\n", file);
  208.       return (1);
  209.     }
  210.   }
  211.   else
  212.   { /* append CRC to file */
  213.     in = fopen (file, OPEN_APPEND_TEXT);
  214.     fprintf (in, "CRC %04X%s", crc, delimit);
  215.     fclose (in);
  216.   }
  217.   return (0);
  218. }
  219.  
  220. /*
  221. ***
  222. *** Write a byte to file.
  223. ***
  224.  */
  225.  
  226. int my_putc (int outchar, FILE *out)
  227. {
  228.   register x;
  229.  
  230.   if ((x = putc ((char) outchar, out)) == EOF)
  231.   {
  232.     printf ("\007\nWrite error! Can't continue.\n");
  233.     exit (1);
  234.   }
  235.   return (x);
  236. }
  237.  
  238. /*
  239. ***
  240. ***
  241. ***
  242.  */
  243. void kill_dest (FILE *in, FILE *out, char *name)
  244. {
  245.     if (out)
  246.       fclose (out);
  247.     if (in)
  248.       fclose (in);
  249.     if (*name)
  250.       unlink (name);
  251. }
  252.  
  253. /*
  254. ***
  255. ***  test if a file exists at all
  256. ***
  257.  */
  258.  
  259. int test_exist (char *filename)
  260. {
  261.   FILE *rein = NULL;
  262.  
  263.   if ((rein = fopen (filename, OPEN_READ_TEXT)) != NULL)
  264.   {
  265.     fclose (rein);
  266.     return (0);
  267.   }
  268.   return (1);
  269. }
  270.  
  271.  
  272. /*
  273. ***  test if outputfile already exists. prompt for overwrite or
  274. ***  new name.
  275. ***
  276. ***
  277.  */
  278.  
  279. void test_file (FILE *rein, char *destnam, int flag, int namsize)
  280. {
  281.    FILE *raus;
  282.    char compare[30], no[] = NO, yes[] = YES;
  283.    int i;
  284.    
  285.    if (noquery)
  286.      return;
  287.  
  288.    sprintf (compare, "%%%d[^\n]", namsize);
  289.  
  290.    /* Loop as long as file can be opened. */
  291.    while ((raus = fopen (destnam, OPEN_READ_BINARY)) != NULL)
  292.    {
  293.      printf ("\007\nOutputfile '%s' already exists, overwrite? [y/n] ", destnam);
  294.      do
  295.      {
  296.        i = getch ();
  297.        if (i == 'n' || i == 'N')
  298.        {
  299.          if (flag)
  300.          {
  301.            printf ("%s\n\nEnter new name (max %d chars)\n", no, namsize);
  302.            printf ("or simply press ENTER to break : ");
  303.            fflush (stdin);
  304.            if (namsize == 12)
  305.              strlwr (destnam);
  306.            scanf (compare, destnam);
  307.            fflush (stdin);
  308.          }
  309.          else
  310.            *destnam = EOS;
  311.          if (!strlen (destnam))
  312.          {
  313.            if (!flag)
  314.              printf ("%s\n", no);
  315.            printf ("Break.\n");
  316.            fclose (rein);
  317.            exit (10);
  318.          }
  319.          i = 0xff; /* indicate, that new name has been specified */
  320.        }
  321.      }
  322.      while (i != 'y' && i != 'Y' && i != 0xff);
  323.      if (i != 0xff)
  324.        printf ("%s\n", yes);
  325.      printf ("\n");
  326.      fclose (raus);
  327.      if (i != 0xff)
  328.        break;
  329.    }
  330. }
  331.  
  332.  
  333. /*
  334. *** initialize decoding table
  335. ***
  336. ***
  337. ***
  338.  */
  339.  
  340. void init_decodetab (void)
  341. {
  342.   register i;
  343.   register byte j;
  344.  
  345.   for (i = 0; i < 256; i++)
  346.     decode[i] = 255;
  347.  
  348.   j = 0;
  349.   for (i = 0x21; i < 0x2a; i++)
  350.     decode[i] = j++;
  351.  
  352.   for (i = 0x2b; i < 0x7f; i++)
  353.     decode[i] = j++;
  354.  
  355.   for (i = 0x80; i < 0x91; i++)
  356.     decode[i] = j++;
  357.  
  358.   decode[0x92] = j++;
  359.  
  360.   for (i = 0x94; i < 0xfd; i++)
  361.     decode[i] = j++;
  362. }
  363.  
  364. /*
  365. *** initialize encoding table
  366. ***
  367. ***
  368. ***
  369.  */
  370.  
  371. void init_codetab (void)
  372. {
  373.   register byte i, j;
  374.  
  375.   j = 0;
  376.  
  377.   for (i = 0x21; i < 0x2a; i++, j++)
  378.     code[j] = i;
  379.  
  380.   for (i = 0x2b; i < 0x7f; i++, j++)
  381.     code[j] = i;
  382.  
  383.   for (i = 0x80; i < 0x91; i++, j++)
  384.     code[j] = i;
  385.  
  386.   code[j++] = 146;
  387.  
  388.   for (i = 0x94; i < 0xfd; i++, j++)
  389.     code[j] = i;
  390. }
  391.  
  392. /*
  393. *** Tnx to DC4OX.
  394. ***
  395. *** calculate CRC-table
  396. ***
  397.  */
  398.  
  399. void init_crctab (void)
  400. {
  401.   uint m, n, r, mask;
  402.  
  403.   static uint bitrmdrs[] = { 0x9188,0x48C4,0x2462,0x1231,
  404.                              0x8108,0x4084,0x2042,0x1021 };
  405.  
  406.   for (n = 0; n < 256; ++n)
  407.   {
  408.     for (mask = 0x0080, r = 0, m = 0; m < 8; ++m, mask >>= 1)
  409.       if (n & mask)
  410.         r = bitrmdrs[m] ^ r;
  411.     crctab[n] = r;
  412.   }
  413. }
  414.  
  415. /*
  416. *** Create a MSDOS/ATARI compatible filename.
  417. ***
  418. ***
  419. ***
  420.  */
  421.  
  422. void build_DOS_name (char *name)
  423. {
  424.   char tmp[MAXFNAME];
  425.   register i, j;
  426.  
  427.   i = j = 0;
  428.  
  429.   strcpy (tmp, name);
  430.   strlwr (tmp);
  431.  
  432.   if (*tmp)
  433.   {
  434.     do
  435.     {
  436.       tmp[i] &= 127;
  437.       if (strchr (" <>=,;:*?&[]()/.\\\"~+@", tmp[i]) == NULL)
  438.         name[j++] = tmp[i];
  439.     }
  440.     while (tmp[++i]);
  441.  
  442.     name[j] = EOS;
  443.   }
  444. }
  445.  
  446. /*
  447. *** get_hex: some compilers have real big trouble when reading hex values from
  448. ***          a file with fscanf() that have leading zeros! e.g. 00A will be
  449. ***          read as two separate values (0 and A)! grr!!
  450. ***          get_hex skips all leading zeros to eliminate the problem.
  451. ***
  452.  */
  453. uint get_hex (char *hex)
  454. {
  455.   register i = 0;
  456.   uint   ret = 0;
  457.  
  458.   while (hex[i] == '0')
  459.     i++;
  460.   sscanf(hex+i, "%x", &ret);
  461.   return (ret);
  462. }
  463.  
  464.  
  465. #ifdef _FNSPLIT
  466. /*
  467. ***       filenamesplit
  468. ***       (by DL1MEN, taken from SP-ST, modified for portability)
  469. ***
  470. ***       split filename up into drive, path, name and extension.
  471. ***
  472.  */
  473.  
  474. void fnsplit(char *pth, char *dr, char *pa, char *fn, char *ft)
  475. {
  476.    char drv[MAXDRIVE], pat[MAXDIR], fna[MAXFILE], fty[MAXEXT], tmp[MAXPATH];
  477.    char *p;
  478.  
  479.    strcpy(tmp,pth);
  480.   
  481.    if ((p = strchr(tmp,':')) != NULL)
  482.    {
  483.      *p++ = EOS;
  484.      strcpy(drv,tmp);
  485.    }
  486.    else
  487.    {
  488.      p = tmp;
  489.      drv[0] = EOS;
  490.    }
  491.    if ((pth = strrchr(p, PATHCHAR)) != NULL)
  492.    {
  493.      *pth++ = EOS;
  494.      strcpy(pat,p);
  495.    }
  496.    else
  497.    {
  498.      pth = p;
  499.      pat[0] = EOS;
  500.    }
  501.    if ((p = strchr(pth,'.')) != NULL)
  502.    {
  503.      strcpy(fty,p);
  504.      fty[MAXEXT-1] = EOS;
  505.      *p = EOS;
  506.    }
  507.    else
  508.      fty[0] = EOS;
  509.  
  510.    strcpy(fna,pth);
  511.    fna[MAXFILE-1] = EOS;
  512.  
  513.    if (dr)
  514.    {
  515.      strcpy(dr,drv);
  516.      if (drv[0])
  517.        strcat(dr,":");
  518.    }
  519.    if (pa)
  520.    {
  521.      strcpy(pa,pat);
  522.      if (pat[0])
  523.        strcat(pa, PATHSEP);
  524.    }
  525.    if (fn)
  526.      strcpy(fn,fna);
  527.    if (ft)
  528.      strcpy(ft,fty);
  529. }
  530. #endif /** _FNSPLIT **/
  531.  
  532. #ifdef _ICMP
  533. /* The following functions are unfortunately not avialable on Aztec 5.0b,
  534.    because it sticks closely to the ANSI_C standard. You may also have
  535.    to include these functions, if compiling on other ANSI_C compilers.*/
  536.  
  537. /*
  538. *** strupr - convert string to upper case.
  539. ***
  540. ***
  541.  */
  542. char *strupr (char *string)
  543. {
  544.   char *strcnvt (char *string, int flag);
  545.  
  546.   return (strcnvt (string, 1));
  547. }
  548.  
  549. /*
  550. *** strlwr - convert string to lower case.
  551. ***
  552. ***
  553.  */
  554. char *strlwr (char *string)
  555. {
  556.   char *strcnvt (char *string, int flag);
  557.  
  558.   return (strcnvt (string, 0));
  559. }
  560.  
  561. /*
  562. *** strcnvt - convert string to upper (flag == 1) or lower (flag == 0) case.
  563. ***
  564. ***
  565.  */
  566. char *strcnvt (char *string, int flag)
  567. {
  568.   register i = 0;
  569.  
  570.   while (string[i])
  571.   {  
  572.     string[i] = (flag)?toupper (string[i]):tolower (string[i]);
  573.     i++;
  574.   }
  575.  
  576.   return (string);
  577. }
  578.  
  579. /*
  580. *** stricmp - same as strcmp(), but ignores case.
  581. *** s1 and s2 are not modified.
  582. ***
  583.  */
  584. int stricmp (char *s1, char *s2)
  585. {
  586.   return (strnicmp (s1, s2, (size_t) 80));
  587. }
  588.  
  589. /*
  590. *** strnicmp - same as strncmp(), but ignores case.
  591. *** s1 and s2 are not modified.
  592. ***
  593.  */
  594. int strnicmp (char *s1, char *s2, int n)
  595. {
  596.   char _s1[81], _s2[81];
  597.  
  598.   strncpy (_s1, s1, 80);
  599.   strncpy (_s2, s2, 80);
  600.   strupr (_s1);
  601.   strupr (_s2);
  602.  
  603.   return (strncmp (_s1, _s2, n));
  604. }
  605. #endif /** _ICMP **/
  606.